package cn.community.system.service.impl;

import cn.community.system.domain.*;
import cn.community.system.exception.LogicException;
import cn.community.system.mapper.*;
import cn.community.system.query.ActivityRecordQuery;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IActivityRecordService;
import cn.community.system.util.UserContext;
import cn.community.system.vo.ActivityVo;
import cn.community.system.vo.ActivityRecordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRecordServiceImpl implements IActivityRecordService {
    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberVoMapper memberVoMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private AssociationMapper associationMapper;

    @Override
    public void delete(Long id) {
        ActivityRecord activityRecord = activityRecordMapper.selectByPrimaryKey(id);
        Activity activity = activityMapper.selectByPrimaryKey(activityRecord.getActivityId());
        // 报名人数减少
        activityMapper.decreate(activity.getNowperson(),activity.getId());
        activityRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void cancelApply(Long id) {
        Activity activity = activityMapper.selectByPrimaryKey(id);
        // 报名人数减少
        activityMapper.decreate(activity.getNowperson(),activity.getId());

        Member member = UserContext.getCurrentMember();
        ActivityRecord activityRecord = activityRecordMapper.getByActivityIdAndMemId(id,member.getId());
        activityRecordMapper.deleteByPrimaryKey(activityRecord.getId());
    }

    @Override
    public ActivityRecord getByIds(Long activityId, Long applicantId) {
        return activityRecordMapper.getByActivityIdAndMemId(activityId,applicantId);
    }

    @Override
    public void save(ActivityRecordVo activityRecord) {
        // 判断活动所需人数是否达到上限
        Long activityId = activityRecord.getActivityId();
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        if (activity.getPerson() <= activity.getNowperson()){
            throw new LogicException("该活动已达人数上限！");
        }
        ActivityRecord record = new ActivityRecord();
        // 社团
        Long associationId = activityRecord.getAssociationId();
        Association association = associationMapper.selectByPrimaryKey(associationId);
        record.setAssociationId(associationId);
        record.setAssociation(association.getUsername());
        // 活动
        record.setActivityId(activityId);
        record.setActivityName(activity.getTitle());
        record.setAllTime(activity.getAllTime());
        // 报名者
        String applicant = activityRecord.getApplicant();
        record.setApplicant(applicant);
        if(memberMapper.selectByName(applicant) == null){
            throw new LogicException("请输入正确的名字！");
        }
        Member member = memberMapper.getUserByNumber(activityRecord.getNumber());
        if(member == null){
            throw new LogicException("请输入正确的学号！");
        }
        record.setApplicantId(member.getId());

        record.setStatus(activityRecord.getStatus());
        record.setReason(activityRecord.getReason());
        Date createTime = activityRecord.getCreateTime();
        if(activityMapper.checkCreateTime(createTime,activityId) == null){
            throw new LogicException("该时间不在报名时间内！");
        }
        record.setCreateTime(createTime);
        activityRecordMapper.insert(record);
    }

    @Override
    public void save2(ActivityRecordVo activityRecord) {
        // 判断活动所需人数是否达到上限
        Long activityId = activityRecord.getActivityId();
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        if (activity.getPerson() <= activity.getNowperson()){
            throw new LogicException("该活动已达人数上限！");
        }
        ActivityRecord record = new ActivityRecord();
        // 社团
        Association association = UserContext.getCurrentAssociation();
        record.setAssociation(association.getUsername());
        record.setAssociationId(association.getId());
        // 活动
        record.setActivityId(activityId);
        record.setActivityName(activity.getTitle());
        record.setAllTime(activity.getAllTime());
        // 报名者
        String applicant = activityRecord.getApplicant();
        record.setApplicant(applicant);
        if(memberMapper.selectByName(applicant) == null){
            throw new LogicException("请输入正确的名字！");
        }
        Member member = memberMapper.getUserByNumber(activityRecord.getNumber());
        if(member == null){
            throw new LogicException("请输入正确的学号！");
        }
        record.setApplicantId(member.getId());

        record.setStatus(activityRecord.getStatus());
        record.setReason(activityRecord.getReason());
        Date createTime = activityRecord.getCreateTime();
        if(activityMapper.checkCreateTime(createTime,activityId) == null){
            throw new LogicException("该时间不在报名时间内！");
        }
        record.setCreateTime(createTime);
        activityRecordMapper.insert(record);
    }

    @Override
    public ActivityRecord get(Long id) {
        return activityRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivityRecord> listAll() {
        return activityRecordMapper.selectAll();
    }

    @Override
    public void update(ActivityRecordVo activityRecord) {
        // 判断活动所需人数是否达到上限
        Long activityId = activityRecord.getActivityId();
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        if (activity.getPerson() <= activity.getNowperson()){
            throw new LogicException("该活动已达人数上限！");
        }
        ActivityRecord record = new ActivityRecord();
        record.setId(activityRecord.getId());
        // 社团
        Long associationId = activityRecord.getAssociationId();
        Association association = associationMapper.selectByPrimaryKey(associationId);
        record.setAssociationId(associationId);
        record.setAssociation(association.getUsername());
        // 活动
        record.setActivityId(activityId);
        record.setActivityName(activity.getTitle());
        record.setAllTime(activity.getAllTime());
        // 报名者
        String applicant = activityRecord.getApplicant();
        record.setApplicant(applicant);
        if(memberMapper.selectByName(applicant) == null){
            throw new LogicException("请输入正确的名字！");
        }
        Member member = memberMapper.getUserByNumber(activityRecord.getNumber());
        if(member == null){
            throw new LogicException("请输入正确的学号！");
        }
        record.setApplicantId(member.getId());

        Integer status = activityRecord.getStatus();
        // 判断状态
        if(status == ActivityRecord.APPLY_NO){
            // 回滚报名人数
            activityMapper.decreate(activity.getNowperson(),activityId);
        }
        record.setStatus(status);
        record.setReason(activityRecord.getReason());
        record.setCreateTime(activityRecord.getCreateTime());

        activityRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public void update2(ActivityRecordVo activityRecord) {
        // 判断活动所需人数是否达到上限
        Long activityId = activityRecord.getActivityId();
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        if (activity.getPerson() <= activity.getNowperson()){
            throw new LogicException("该活动已达人数上限！");
        }
        ActivityRecord record = new ActivityRecord();
        record.setId(activityRecord.getId());
        // 活动
        record.setActivityId(activityId);
        record.setActivityName(activity.getTitle());
        record.setAllTime(activity.getAllTime());
        // 报名者
        String applicant = activityRecord.getApplicant();
        record.setApplicant(applicant);
        if(memberMapper.selectByName(applicant) == null){
            throw new LogicException("请输入正确的名字！");
        }
        Member member = memberMapper.getUserByNumber(activityRecord.getNumber());
        if(member == null){
            throw new LogicException("请输入正确的学号！");
        }
        record.setApplicantId(member.getId());

        Integer status = activityRecord.getStatus();
        // 判断状态
        if(status == ActivityRecord.APPLY_NO){
            // 回滚报名人数
            activityMapper.decreate(activity.getNowperson(),activityId);
        }
        record.setStatus(status);
        record.setReason(activityRecord.getReason());
        record.setCreateTime(activityRecord.getCreateTime());

        activityRecordMapper.updateByPrimaryKey(record);
    }

    public PageInfo query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询结果集
        List<ActivityRecordVo> list = activityRecordMapper.selectForList(qo);
        for (ActivityRecordVo vo : list) {
            MemberVo memberVo = memberVoMapper.selectByMemberIdAndAssoId(vo.getApplicantId(), vo.getAssociationId());
            vo.setMyclub(memberVo == null?'否':'是');
        }
        return new PageInfo(list);
    }

    public PageInfo queryAsso(ActivityRecordQuery qo) {
        Association association = UserContext.getCurrentAssociation();
        qo.setAssoId(association.getId());
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"createTime desc");
        //查询结果集
        List<ActivityRecordVo> list = activityRecordMapper.selectForAssoList(qo);
        for (ActivityRecordVo vo : list) {
            MemberVo memberVo = memberVoMapper.selectByMemberIdAndAssoId(vo.getApplicantId(), association.getId());
            vo.setMyclub(memberVo == null?'否':'是');
        }
        return new PageInfo(list);
    }

    public PageInfo queryMember(ActivityRecordQuery qo) {
        Member member = UserContext.getCurrentMember();
        qo.setMemId(member.getId());
        //使用分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"a.createTime desc");
        //查询结果集
        List<ActivityRecordVo> list = activityRecordMapper.selectForMemList(qo);
        return new PageInfo(list);
    }

    @Override
    public void passOne(Long id, Integer status) {
        activityRecordMapper.passOne(id,status);
    }

    @Override
    public void refuseOne(ActivityRecord activityRecord) {
        // 改变报名状态
        activityRecordMapper.refuseOne(activityRecord);
        // 回滚报名人数
        Long activityId = activityRecord.getActivityId();
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        activityMapper.decreate(activity.getNowperson(),activityId);
    }

    @Override
    public void batchPass(Long[] ids) {
        for (Long id : ids) {
            this.passOne(id,ActivityRecord.APPLY_YES);
        }
    }

    @Override
    public ActivityRecordVo getVo(Long id) {
        return activityRecordMapper.getVo(id);
    }

    @Override
    public List<ActivityRecord> getByApplySuccess(Long id) {
        return activityRecordMapper.getByApplySuccess(id);
    }

    @Override
    public void applyActivity(Long id) {
        Member member = UserContext.getCurrentMember();
        Activity activity = activityMapper.selectByPrimaryKey(id);
        ActivityRecord record = activityRecordMapper.getByActivityIdAndMemId(id, member.getId());
        if(record != null){
            if(record.getStatus() == ActivityRecord.APPLY_YES){
                throw new LogicException("已报名成功！");
            }else if(record.getStatus() == ActivityRecord.APPLY_NO){
                throw new LogicException("报名失败，请查看报名记录中的失败原因！");
            }
        }

        // 判断是否符合报名时间
        long now = new Date().getTime();
        if (activity.getApplyStartTime().getTime() >= now && activity.getApplyEndTime().getTime() <= now){
            throw new LogicException("该活动不在报名时间内！");
        }
        // 判断是否达到活动上线人数
        if (activity.getPerson() <= activity.getNowperson()){
            throw new LogicException("该活动已达人数上限！");
        }

        // 是否需要满足以下条件：1、报名的只能是某院校或某专业的 2、报名的只能是本社团的 3、

        // 新增活动报名记录
        ActivityRecord activityRecord = new ActivityRecord();

        activityRecord.setAssociation(activity.getSponsor());
        activityRecord.setAssociationId(activity.getSponsorId());

        activityRecord.setActivityName(activity.getTitle());
        activityRecord.setActivityId(activity.getId());

        activityRecord.setApplicant(member.getUsername());
        activityRecord.setApplicantId(member.getId());

        activityRecord.setCreateTime(new Date());
        activityRecord.setAllTime(activity.getAllTime());
        activityRecord.setStatus(ActivityRecord.APPLYING);

        activityRecordMapper.insert(activityRecord);

        // 报名人数增加
        activityMapper.increate(activity.getNowperson(),activity.getId());
    }

}
